package com.wwwarehouse.usercenter.fragment.register;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wei.android.lib.fingerprintidentify.FingerprintIdentify;
import com.wei.android.lib.fingerprintidentify.base.BaseFingerprint;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.tools.DialogTools;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.usercenter.R;

import de.greenrobot.event.EventBus;

import static com.wwwarehouse.usercenter.constant.UserCenterConstant.sp_Finger;

/**
 * Created by Jackie on 2017/8/15.
 * 指纹解锁设置
 */
public class FingerUnlockSettingsFragment extends RegisterCardBaseFragment implements View.OnClickListener {
    private View mRootView;
    private ImageView mFingerImg;
    private TextView mDescriptionTxt;
    private Button mStartBtn;

    private FingerprintIdentify mFingerprintIdentify;

    private int MAX_AVAILABLE_TIMES = 5;  //识别10次就自动结束识别

    private boolean mIsFingerStarted = false;  //是否已经开启指纹解锁

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_finger_unlock_settings, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initEvent();
    }

    private void initView() {
        mFingerImg = findView(mRootView, R.id.iv_finger_unlock);
        mDescriptionTxt = findView(mRootView, R.id.tv_finger_description);
        mStartBtn = findView(mRootView, R.id.btn_finger_start);

        mIsFingerStarted = BaseApplication.getApplicationInstance().sp.getBooleanValue(sp_Finger, false);

        mFingerprintIdentify = new FingerprintIdentify(BaseApplication.getApplicationInstance(), new BaseFingerprint.FingerprintIdentifyExceptionListener() {
            @Override
            public void onCatchException(Throwable exception) {
                LogUtils.showLog(exception.getLocalizedMessage());
            }
        });

        if (mFingerprintIdentify.isFingerprintEnable()) {
            if (!mIsFingerStarted) {
                mFingerImg.setImageResource(R.drawable.finger_unlock_settings);
                mDescriptionTxt.setText(R.string.finger_unlock_description);
                mStartBtn.setText(R.string.start_finger_unlock);
                mStartBtn.setEnabled(true);
                mStartBtn.setBackgroundResource(R.drawable.common_btn2_solid_selector);
            } else {
                mFingerImg.setImageResource(R.drawable.finger_unlock_finish);
                mDescriptionTxt.setText(R.string.finger_has_started);
                mStartBtn.setText(R.string.close_finger_unlock);
                mStartBtn.setEnabled(true);
                mStartBtn.setBackgroundResource(R.drawable.common_btn_solid_selector);
            }
        }
    }

    private void initEvent() {
        mStartBtn.setOnClickListener(this);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof FingerUnlockSettingsFragment) {
            mActivity.setTitle(R.string.fingerprint_unlock_settings);
        }
    }

    @Override
    public void requestDatas() {

    }

    private TextView mTvTimes;
    private Dialog mDialogFinger;
    class DialogRunnable implements Runnable{

        @Override
        public void run() {
            DialogTools.getInstance().showCustomStartFinger(mActivity, false, new DialogTools.ConfirmListener() {
                @Override
                public void setConfirmListener(Dialog dialog, View view, String input) {
                    mDialogFinger = dialog;
                    mTvTimes = (TextView) (view).findViewById(R.id.tv_content_finger);
                }
            }, new DialogTools.DismissListener() {
                @Override
                public void setDismissListener(Dialog dialog, View view) {
                    dialog.dismiss();
                    mFingerprintIdentify.cancelIdentify();
                }
            });
        }
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_finger_start) {
            mIsFingerStarted = BaseApplication.getApplicationInstance().sp.getBooleanValue(sp_Finger, false);

            if (!mIsFingerStarted) {

                final Handler handler = new Handler();
                final DialogRunnable dialogRunnable = new DialogRunnable();
                handler.postDelayed(dialogRunnable, 200);
                //开启指纹解锁
                mFingerprintIdentify.startIdentify(MAX_AVAILABLE_TIMES, new BaseFingerprint.FingerprintIdentifyListener() {
                    @Override
                    public void onSucceed() {
                        //指纹识别成功
                        mDialogFinger.dismiss();

                        mFingerImg.setImageResource(R.drawable.finger_unlock_finish);
                        mDescriptionTxt.setText(R.string.finger_has_started);
                        mStartBtn.setText(R.string.close_finger_unlock);
                        mStartBtn.setEnabled(true);
                        mStartBtn.setBackgroundResource(R.drawable.common_btn_solid_selector);

                        sp.putBooleanValue(sp_Finger, true);
                        EventBus.getDefault().post("FingerUnlockSettingsFragment");
                        popFragment();
                    }

                    @Override
                    public void onNotMatch(int availableTimes) {
                        mTvTimes.setText(getResources().getString(R.string.wrong)+availableTimes+getResources().getString(R.string.times));
                        LogUtils.showLog("availableTimes: " + availableTimes);

                    }

                    @Override
                    public void onFailed(boolean isDeviceLocked) {
                        LogUtils.showLog("onFailed          "+isDeviceLocked);
                        mDialogFinger.dismiss();
                        toast(mActivity.getResources().getString(R.string.finger_waite));
                        handler.removeCallbacks(dialogRunnable);
                    }

                    @Override
                    public void onStartFailedByDeviceLocked() {
                        LogUtils.showLog("onStartFailedByDeviceLocked");
                        mFingerprintIdentify.cancelIdentify();
                        toast(mActivity.getResources().getString(R.string.finger_waite));
                        handler.removeCallbacks(dialogRunnable);
                    }
                });

            } else {
                //关闭指纹解锁
                DialogTools.getInstance().showConfirmCloseFinger(mActivity, true, new DialogTools.ConfirmListener() {
                    @Override
                    public void setConfirmListener(Dialog dialog, View view, String input) {
                        dialog.dismiss();

                        mFingerImg.setImageResource(R.drawable.finger_unlock_settings);
                        mDescriptionTxt.setText(R.string.finger_unlock_description);
                        mStartBtn.setText(R.string.start_finger_unlock);
                        mStartBtn.setEnabled(true);
                        mStartBtn.setBackgroundResource(R.drawable.common_btn2_solid_selector);

                        sp.putBooleanValue(sp_Finger, false);
                        EventBus.getDefault().post("FingerUnlockSettingsFragment");
                        popFragment();
                    }
                });
            }
        }
    }
}
